Option Explicit On Option Strict On Public Class Form1 Inherits System.Windows.Forms.Form ' alternative ==> Do Until guess = actual ' alternative - post test ==> Do Const min As Integer = 1 Const max As Integer = 100 #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents BtnQuit As System.Windows.Forms.Button Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TxtGuesses As System.Windows.Forms.TextBox Friend WithEvents BtnLoopUntil As System.Windows.Forms.Button Friend WithEvents BtnLoopWhile As System.Windows.Forms.Button Friend WithEvents BtnDoWhile As System.Windows.Forms.Button Friend WithEvents BtnDoUntil As System.Windows.Forms.Button Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.BtnDoUntil = New System.Windows.Forms.Button Me.BtnQuit = New System.Windows.Forms.Button Me.Label2 = New System.Windows.Forms.Label Me.TxtGuesses = New System.Windows.Forms.TextBox Me.BtnLoopUntil = New System.Windows.Forms.Button Me.BtnLoopWhile = New System.Windows.Forms.Button Me.BtnDoWhile = New System.Windows.Forms.Button Me.SuspendLayout() ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(113, 21) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(193, 31) Me.Label1.TabIndex = 0 Me.Label1.Text = "Guessing Game" ' 'BtnDoUntil ' Me.BtnDoUntil.Location = New System.Drawing.Point(32, 40) Me.BtnDoUntil.Name = "BtnDoUntil" Me.BtnDoUntil.Size = New System.Drawing.Size(67, 35) Me.BtnDoUntil.TabIndex = 1 Me.BtnDoUntil.Text = "Start do until" ' 'BtnQuit ' Me.BtnQuit.Location = New System.Drawing.Point(168, 80) Me.BtnQuit.Name = "BtnQuit" Me.BtnQuit.Size = New System.Drawing.Size(67, 35) Me.BtnQuit.TabIndex = 2 Me.BtnQuit.Text = "Quit" ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.Location = New System.Drawing.Point(40, 215) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(147, 35) Me.Label2.TabIndex = 3 Me.Label2.Text = "Number of Guesses" ' 'TxtGuesses ' Me.TxtGuesses.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.TxtGuesses.Location = New System.Drawing.Point(200, 215) Me.TxtGuesses.Name = "TxtGuesses" Me.TxtGuesses.ReadOnly = True Me.TxtGuesses.Size = New System.Drawing.Size(87, 23) Me.TxtGuesses.TabIndex = 4 Me.TxtGuesses.Text = "" ' 'BtnLoopUntil ' Me.BtnLoopUntil.Location = New System.Drawing.Point(32, 160) Me.BtnLoopUntil.Name = "BtnLoopUntil" Me.BtnLoopUntil.Size = New System.Drawing.Size(67, 35) Me.BtnLoopUntil.TabIndex = 5 Me.BtnLoopUntil.Text = "Start loop until" ' 'BtnLoopWhile ' Me.BtnLoopWhile.Location = New System.Drawing.Point(32, 120) Me.BtnLoopWhile.Name = "BtnLoopWhile" Me.BtnLoopWhile.Size = New System.Drawing.Size(67, 35) Me.BtnLoopWhile.TabIndex = 6 Me.BtnLoopWhile.Text = "Start loop while" ' 'BtnDoWhile ' Me.BtnDoWhile.Location = New System.Drawing.Point(32, 80) Me.BtnDoWhile.Name = "BtnDoWhile" Me.BtnDoWhile.Size = New System.Drawing.Size(67, 35) Me.BtnDoWhile.TabIndex = 7 Me.BtnDoWhile.Text = "Start do while" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(533, 388) Me.Controls.Add(Me.BtnDoWhile) Me.Controls.Add(Me.BtnLoopWhile) Me.Controls.Add(Me.BtnLoopUntil) Me.Controls.Add(Me.TxtGuesses) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.BtnQuit) Me.Controls.Add(Me.BtnDoUntil) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub BtnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuit.Click Me.Close() End Sub Private Sub BtnDoUntil_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDoUntil.Click Dim guess As Integer = -1 ' initialize to something guarenteed to be wrong so go into loop the first time Dim guessText As String Dim numGuesses As Integer = 0 Dim actual As Integer ' create a number to guess (and display it for testing purposes) actual = Convert.ToInt32((max - min + 1) * Rnd() + min) ' MsgBox("the actual is: " & actual) guessText = InputBox("I'm Thinking of a Number Between " & min & " and " & max, "Guess") If IsNumeric(guessText) Then ' valid entry guess = Convert.ToInt32(guessText) Do Until guess = actual numGuesses = numGuesses + 1 If guess = actual Then 'got it right TxtGuesses.Text = numGuesses.ToString("n0") ElseIf guess > actual Then MsgBox("Your guess is too high") Else MsgBox("Your guess is too low") End If guessText = InputBox("I'm Thinking of a Number Between " & min & " and " & max, "Guess") If IsNumeric(guessText) Then ' valid entry guess = Convert.ToInt32(guessText) /* Bug fix! */ numGuesses = numGuesses + 1 End If Loop TxtGuesses.Text = numGuesses.ToString("n0") End If End Sub Private Sub BtnLoopUntil_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLoopUntil.Click Dim guess As Integer = -1 ' initialize to something guarenteed to be wrong so go into loop the first time Dim guessText As String Dim numGuesses As Integer = 0 Dim actual As Integer ' create a number to guess (and display it for testing purposes) actual = Convert.ToInt32((max - min + 1) * Rnd() + min) ' MsgBox("the actual is: " & actual) Do guessText = InputBox("I'm Thinking of a Number Between " & min & " and " & max, "Guess") 'inputBox( If IsNumeric(guessText) Then ' valid entry numGuesses = numGuesses + 1 guess = Convert.ToInt32(guessText) If guess = actual Then 'got it right TxtGuesses.Text = numGuesses.ToString("n0") ElseIf guess > actual Then MsgBox("Your guess is too high") Else MsgBox("Your guess is too low") End If End If Loop Until guess = actual End Sub Private Sub BtnDoWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDoWhile.Click Dim guess As Integer = -1 ' initialize to something guarenteed to be wrong so go into loop the first time Dim guessText As String Dim numGuesses As Integer = 0 Dim actual As Integer ' create a number to guess (and display it for testing purposes) actual = Convert.ToInt32((max - min + 1) * Rnd() + min) ' MsgBox("the actual is: " & actual) guessText = InputBox("I'm Thinking of a Number Between " & min & " and " & max, "Guess") If IsNumeric(guessText) Then ' valid entry guess = Convert.ToInt32(guessText) Do While guess <> actual numGuesses = numGuesses + 1 If guess = actual Then 'got it right TxtGuesses.Text = numGuesses.ToString("n0") ElseIf guess > actual Then MsgBox("Your guess is too high") Else MsgBox("Your guess is too low") End If guessText = InputBox("I'm Thinking of a Number Between " & min & " and " & max, "Guess") If IsNumeric(guessText) Then ' valid entry guess = Convert.ToInt32(guessText) /* Bug fix! */ numGuesses = numGuesses + 1 End If Loop TxtGuesses.Text = numGuesses.ToString("n0") End If End Sub Private Sub BtnLoopWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLoopWhile.Click Dim guess As Integer = -1 ' initialize to something guarenteed to be wrong so go into loop the first time Dim guessText As String Dim numGuesses As Integer = 0 Dim actual As Integer ' create a number to guess (and display it for testing purposes) actual = Convert.ToInt32((max - min + 1) * Rnd() + min) ' MsgBox("the actual is: " & actual) Do guessText = InputBox("I'm Thinking of a Number Between " & min & " and " & max, "Guess") 'inputBox( If IsNumeric(guessText) Then ' valid entry numGuesses = numGuesses + 1 guess = Convert.ToInt32(guessText) If guess = actual Then 'got it right TxtGuesses.Text = numGuesses.ToString("n0") ElseIf guess > actual Then MsgBox("Your guess is too high") Else MsgBox("Your guess is too low") End If End If Loop While actual <> guess End Sub End Class